AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:DealDamage( amount, attacker )
	if ( not (self.Entity and self.Entity:IsValid()) ) then return end
	if self.health - amount > 0 then
		self.health = self.health - amount
		self.Entity:SetNWInt( "health", self.health )
		if !self:GetOverlord() then return end
		if !self:IsReady() then return end
		local r,g,b,a = self:GetOverlord():GetColor()
		local d = self.clamp - ( self.clamp * math.Clamp( self.health , 0 , self.maxhealth ) / self.maxhealth )
		r = r + d
		g = g + d
		b = b + d
		self.Entity:SetColor( r,g,b,a )
	else
		self:Raze()
		attacker.Clock = true
		if attacker:GetClass() == "unit" then
			attacker.combatant = false
		elseif attacker:GetClass() == "arrow" then
			attacker.Predict = false
			attacker:GetPhysicsObject():EnableGravity( true )
			attacker:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
			local phys = attacker:GetPhysicsObject()
			phys:EnableMotion(true)
			phys:Wake()
		end
	end
end

function ENT:GetClamp( r, g, b )
	local function winner( n, m )
		if m > n then
			return m
		else
			return n
		end
	end
	local small = winner( r, g )
	small = winner( small, b )
	local clamp = 255 - small
	self.clamp = clamp
end

function ENT:IsDead()
	if !self.Dead then
		return false
	elseif self.Dead and self.Spawning then
		return false
	elseif self.Dead then
		return true
	end
end

function ENT:IsReady()
	if self.Dead then
		return false
	else
		return true
	end
end

function ENT:IsUpgraded()
	return self.upgraded
end

function ENT:GetOverlord()
	return self.Overlord
end